using System;
using System.Collections.Generic;
using System.Text;

namespace Kassandra.Core
{
    public interface ICustomWordWrapper
    {
        IList<string> WrapText(string text);
    }
    
    public class CustomWordWrapper : ICustomWordWrapper 
    {
        private readonly int wraplimit;

        public CustomWordWrapper(int wraplimit)
        {
            this.wraplimit = wraplimit;
        }

        public IList<string> WrapText(string text)
        {
            IList<string> lines = new List<string>();

            string currentLine = "";
            foreach (string word in text.Split(' '))
            {
                if (currentLine.Length + word.Length <= wraplimit)
                {
                    currentLine += word + " ";    
                }
                else
                {
                    lines.Add(currentLine);
                    currentLine = word + " ";
                }
            }
            if (currentLine.Trim().Length > 0 )
                lines.Add(currentLine); //add the last bit of text that gets missed in the loop
            return lines;
        }
    }
}
